#include "filesystem.h"
#include "directory.h"
#include "blockFreeList.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

void createTestDir(void);
void fileTest(void);
void testMatch(void);
void testGetDir(void);
void testBitMap(void);
void writeTest(void);
void readTest(void);
void testBlockUsed(void);

int main(){
  createTestDir();
  fileTest();
  testMatch();
  testGetDir();
  testBitMap();
  writeTest();
  readTest();
  testBlockUsed();
  return 0;
}



void createTestDir(){
  root = malloc(sizeof(directoryNode_t));
  root->name = "/";
  root->inodeNumber = 14;
  int rootchildnum = root->numberOfChildren;
  directoryNode_t* dir = malloc(sizeof(directoryNode_t));
  dir->name = "cookie";
  dir->inodeNumber = 7;
  addChild(root, dir);
  dir = malloc(sizeof(directoryNode_t));
  dir->name = "taco";
  dir->inodeNumber = 94;
  int tacochildnum = dir->numberOfChildren;
  addChild(root, dir);
  directoryNode_t* newdir = malloc(sizeof(directoryNode_t));
  newdir->name = "cat";
  dir->inodeNumber = 4582;
  addChild(dir, newdir);

  assert(rootchildnum+2==root->numberOfChildren);
  assert(tacochildnum+1==dir->numberOfChildren);
  assert(!strcmp(root->children[0]->name, "cookie"));
  assert(!strcmp(root->children[1]->name, "taco"));
  assert(!strcmp(root->children[1]->children[0]->name, "cat"));
  assert(!strcmp(root->children[1]->children[0]->parent->name, "taco"));
  assert(root->children[2] == NULL);

  dir = malloc(sizeof(directoryNode_t));
  dir->name = "share";
  dir->inodeNumber = 11;
  addChild(root, dir);
  newdir = malloc(sizeof(directoryNode_t));
  newdir->name="cookie";
  newdir->inodeNumber=10;
  addChild(dir,newdir);

}

void fileTest(){
  file_t* file = openFile("cookie");
  assert(file->inodeNumber == 10);

  file = openFile("noexist");
  assert(!strcmp(file->filepath, "/share/noexist"));

  file = openFile("why");
  file_t* file2 = openFile("why2");
 // assert(file->inodeNumber != file2->inodeNumber);
}

void testMatch(){
  directoryNode_t* dir  = matchDirectory(root, "taco");
  assert(dir != NULL);
  dir = matchDirectory(root, "banjo");
  assert(dir == NULL);
}

void testGetDir(){
  directoryNode_t* dir = getDirectory("/taco/cat");
  assert(!strcmp(dir->name, "cat"));
  dir = getDirectory("/");
  assert(dir == root);
  dir = getDirectory("asfdasfsafkj");
  assert(dir == NULL);
}

void testBitMap(){
	freelist.numberofelements = DISK_SIZE/(sizeof(int) * BITS_PER_BYTE);
	freelist.bitmap[16] = 0x35;
    int num = getUnusedBlockNumber();
	assert(num == 16*32+1);
	freelist.bitmap[16] = INT_MAX;
	freelist.bitmap[17] = INT_MAX;
	freelist.bitmap[18] = 0x9F;
	num = getUnusedBlockNumber();
	assert(num == 18*32+5);
}
void writeTest(){
  file_t* fp = openFile("cookie");
  writeFile(fp, "Hello, World!");

 // fp = openFile("pookie");
  //writeFile(fp, "asdffj asdl;fjkas fdl;kj");

}

void readTest(){
	file_t* fp = openFile("cookie");
	char* contents;
	contents = readFile(fp);
	printf("%s\n", contents);

//	fp = openFile("pookie");
//	contents = readFile(fp);
//	printf("%s\n", contents);
}

void testBlockUsed(){
	int blockNum = 25;

}
